package com.jojo.utils;

import com.jojo.entity.Result;
import com.jojo.yaml.Constant;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HttpClientUtil {

    @Autowired
    private Constant constant;

    public Result httpClient(String pid, String cookie) throws Exception {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(constant.cartApi + "?pid=" + pid + "&pcount=1&ptype=1");
        httpGet.addHeader("Cookie", cookie);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        String html = Jsoup.parse(EntityUtils.toString(response.getEntity())).toString();
        if (html.contains("京东-欢迎登录")) {
            httpClient.close();
            return Result.build(-1, "cookie已过期");
        }
        if (html.contains("商品已成功加入购物车")) {
            httpGet = new HttpGet(constant.simulateApi);
            httpGet.addHeader("Cookie", cookie);
            httpGet.addHeader("Referer", "https://item.jd.com/");
            httpGet.addHeader("User-Agent", "PostmanRuntime/7.28.4");
            response = httpClient.execute(httpGet);
            html = Jsoup.parse(EntityUtils.toString(response.getEntity())).toString();
            if (html.contains("京东-欢迎登录")) {
                httpClient.close();
                return Result.build(-1, "cookie已过期");
            }
            if (html.contains("订单结算页 -京东商城")) {
                httpGet = new HttpGet(constant.submitApi);
                httpGet.addHeader("Cookie", cookie);
                httpGet.addHeader("Referer", constant.submitApi);
                httpGet.addHeader("User-Agent", "PostmanRuntime/7.28.4");
                response = httpClient.execute(httpGet);
                html = Jsoup.parse(EntityUtils.toString(response.getEntity())).toString();
                if (html.contains("京东-欢迎登录")) {
                    httpClient.close();
                    return Result.build(-1, "cookie已过期");
                }
                httpClient.close();
                return Result.build(0, "下单成功");
            } else {
                httpClient.close();
                return Result.build(-1, "未知错误，请联系管理员");
            }
        } else {
            httpClient.close();
            return Result.build(-1, "未知错误，请联系管理员");
        }
    }

    public void httpClient(String cookie) throws Exception {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(constant.chooseApi);
        httpGet.addHeader("Cookie", cookie);
        httpGet.addHeader("Referer", "https://cart.jd.com/");
        httpGet.addHeader("User-Agent", "PostmanRuntime/7.28.4");
        httpClient.execute(httpGet);

        httpGet = new HttpGet(constant.simulateApi);
        httpGet.addHeader("Cookie", cookie);
        httpGet.addHeader("Referer", "https://item.jd.com/");
        httpGet.addHeader("User-Agent", "PostmanRuntime/7.28.4");
        httpClient.execute(httpGet);

        httpGet = new HttpGet(constant.submitApi);
        httpGet.addHeader("Cookie", cookie);
        httpGet.addHeader("Referer", constant.submitApi);
        httpGet.addHeader("User-Agent", "PostmanRuntime/7.28.4");
        httpClient.execute(httpGet);
        httpClient.close();
    }
}
